package org.pearl.thymeleaf.demo.controller;

import org.pearl.thymeleaf.demo.pojo.Role;
import org.pearl.thymeleaf.demo.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;

/**
 * Created by TD on 2021/7/17
 */
@Controller
public class ThymeleafController {

    @GetMapping("/index")
    public String index(Model model) {
        model.addAttribute("name", "张三");
        model.addAttribute("msg", "This is <b>great!</b>");
        return "index";
    }

    @GetMapping("/file")
    public String file(Model model) {
        return "file";
    }

    @GetMapping("/user")
    public String user(Model model) {
        // 添加User类到Model
        Role role = Role.builder().roleId(1234L).roleName("管理员").build();
        User user = User.builder().role(role).age(18).birthday(new Date()).email("1113@163.com").money(11.99D).username("张三").build();
        model.addAttribute("user", user);
        return "user";
    }

    @GetMapping("/msg")
    public String msg() {
        return "msg";
    }

    @GetMapping("/text")
    public String text(Model model) {
        model.addAttribute("name", "张三");
        model.addAttribute("name2", "张三");
        model.addAttribute("n1", 10);
        model.addAttribute("n2", 20);
        model.addAttribute("bb", true);
        model.addAttribute("nullKey", null);
        return "text";
    }


    @GetMapping("/list")
    public String list(Model model) {
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Role role = Role.builder().roleId(1234L + i).roleName("管理员" + i).build();
            User user = User.builder().role(role).age(18 + i).birthday(new Date()).email("1113@163.com").money(11.99D).username("张三" + i).build();
            list.add(user);
        }
        model.addAttribute("list", list);
        return "list";
    }

    @GetMapping("/map")
    public String map(Model model) {
        Map<String, Object> map = new HashMap<>();
        map.put("name","韩梅梅");
        map.put("age",100);
        map.put("sex","雌雄同体");
        map.put("class","三年二班");
        map.put("是否在世",true);
        map.put("账户余额",99999.99F);
        model.addAttribute("map", map);


        model.addAttribute("a",true);
        model.addAttribute("b","");
        model.addAttribute("c",null);
        model.addAttribute("d",10);
        model.addAttribute("e",-2);

        // 添加User类到Model
        Role role = Role.builder().roleId(1234L).roleName("管理员").build();
        User user = User.builder().role(role).age(18).birthday(new Date()).email("1113@163.com").money(11.99D).username("张三").build();
        model.addAttribute("user", user);
        return "map";
    }


}

























